package com.wanjia.estate.service.bdservice.bdcdjxxcxjgzmservice;

import com.wanjia.estate.mapper.GyfsMapper;
import com.wanjia.estate.pojo.bdc.Head;
import com.wanjia.estate.pojo.entity.Bdcdjxxjgcx;
import com.wanjia.estate.pojo.entity.Details;
import com.wanjia.estate.pojo.bdc.suit.TcRequest;
import com.wanjia.estate.pojo.bdc.suit.TcResponse;
import com.wanjia.estate.service.httpservice.PropertyHttpService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SearchHomeService {

    @Setter(onMethod_ = @Autowired)
    private PropertyHttpService service;

    @Autowired
    GyfsMapper gyfsMapper;

    public List<Bdcdjxxjgcx> SearchHome(Details detail){
        List<Bdcdjxxjgcx> bdcdjxxjgcxList = new ArrayList<>();
        TcRequest tcrequest = new TcRequest();
        Head head = new Head();
        TcRequest.Data1 data1= new TcRequest.Data1();
        List<TcRequest.Qlr> qlrList = new ArrayList<>();
        tcrequest.setHead(head);
        tcrequest.setData(data1);
        head.setUsername("znblj");
        head.setPassword("9158f2254946d91446504009393652de");
        head.setRegioncode("320101");
        head.setOrgid("tax");
        head.setSize("10");
        head.setPage("0");
        data1.setQlrlist(qlrList);
        TcRequest.Qlr qlr = new TcRequest.Qlr();
        qlr.setQlrzjh(detail.getIdcardno());
        qlr.setQlrmc(detail.getName());
        qlrList.add(qlr);
        log.info("套次请求{}",tcrequest);
        TcResponse tcResponse = service.Cqrfwtccx(tcrequest);
        log.info("套次响应{}",tcResponse);

        //定义权利人套次填表字段
        String fwyt=null;
        String qlrcqzh = null;
        String zl = null;
        String mode=null;
        List<TcResponse.ResponseList> list = tcResponse.getData().getList();
        for (TcResponse.ResponseList responseList : list) {
            //获得本人名下房产编号
            List<TcResponse.Bdclist> bdclist = responseList.getBdclist();
            for (TcResponse.Bdclist bdclist1 : bdclist) {
                log.info("个人套次取出数据{}",bdclist1);
                fwyt = bdclist1.getSjyt();
//                if (fwyt.equals("住宅")){
                    zl = bdclist1.getZl();
                    //获取住宅单元号
                    qlrcqzh = responseList.getCqzh();
//                }
            }
            List<TcResponse.Qlrlist> qlrlist = responseList.getQlrlist();
            for (TcResponse.Qlrlist qlrlist1 : qlrlist) {
                String gyfs = qlrlist1.getGyfs();
                if (gyfs.isEmpty()){
                    mode="未知";
                }else{
                    mode = gyfsMapper.getMode(gyfs);
                }
            }
            Bdcdjxxjgcx bdcdjxxjgcx = new Bdcdjxxjgcx();
            //获取查询人年龄是否成年
            String yearAge = detail.getIdcardno().substring(6, 10);
            String monthAge = detail.getIdcardno().substring(10, 12);
            String dateAge = detail.getIdcardno().substring(12, 14);
            String strBirthday = yearAge + "-" + monthAge + "-" + dateAge;
            LocalDate birthday = LocalDate.parse(strBirthday);
            LocalDate now = LocalDate.now();
            Period period = Period.between(birthday, now);
            int years = period.getYears();
            //判断是查询人还是协查人
            if (years > 18) {
                log.info("查询人");
                bdcdjxxjgcx.setCxr(detail.getName() + " " + detail.getIdcardno());
                bdcdjxxjgcx.setCqzh(qlrcqzh);
                bdcdjxxjgcx.setFwyt(fwyt);
                bdcdjxxjgcx.setZl(zl);
                bdcdjxxjgcx.setGyfs(mode);
                bdcdjxxjgcxList.add(bdcdjxxjgcx);
            }else{
                log.info("协查人");
                bdcdjxxjgcx.setXcr(detail.getName() + " " + detail.getIdcardno());
                bdcdjxxjgcx.setCqzh(qlrcqzh);
                bdcdjxxjgcx.setFwyt(fwyt);
                bdcdjxxjgcx.setZl(zl);
                bdcdjxxjgcx.setGyfs(mode);
                bdcdjxxjgcxList.add(bdcdjxxjgcx);
            }

        }
        return bdcdjxxjgcxList;
    }
}
